<?php
namespace Kuoqi\Html\Page;
class Markdom{

    public $stream = '';

    public function __construct($stream = ''){
        $this->stream = $stream;
    }

    public function flush($type = ''){
        $data = explode("\n",$this->stream);
        foreach($data as $k=>$item){
            $data[$k] =  $this->flushRow($item);
        }
        return implode('',$data);
    }

    //对单行的输出
    public function flushRow($stream){
        //判定是表格的那种，竖划线分割
        //判定第一个是|与最后一个为|
        if(preg_match('/^\s{0,}\|([\s\S]+)\|\s{0,}$/',trim($stream),$rs)){
            //判定为表格的那种，直接输出表格模式
            $cols = explode('|',$rs[1]);
            $stream = '';
            foreach($cols as $v){
                $stream .= '<td>'.$v.'</td>';
            }
            $stream = '<tr>'.$stream.'</tr>';
        }

        return $stream;

    }

    //直接输出为表格
    public function flushWithTable($stream,$headerinfo = array()){
        $data = explode("\n",$stream);
        foreach($data as $k=>$item){
            $data[$k] =  $this->flushRow($item);
        }
        $headstream = '';
        if($headerinfo){
            foreach($headerinfo as $v){
                $headstream .= '<th>'.$v.'</th>';
            }
            $headstream = '<tr>'.$headstream.'</tr>';
        }
        $stream = '<table border="1" cellspacing="1" style="color: #fff;" width="100%">'.$headstream.implode('',$data).'</table>';
        return $stream;
    }
}